<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formEmojiTrait{
    public function index_emoji(Content $content)
    {
        return $content->title('表情配置')->description('所有表情的alt不可重复。')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_emoji()->render()}</div>
HTML
            );
    }
    protected function form_emoji(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        $form->tab('第一页', function (Form $form){
            $form->table('app_emoji_page_1','图片表情', function (NestedForm $table) {
                $table->text('alt', 'alt')->help('会把内容中[坏笑]替换成表情展示')->default('[坏笑]');
                $table->image('emoji', '表情')->help('<a href="https://img.mini.minisns.cn/2023/05/14/cfb8b73b224490acf0375bf3a5f35c4d.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/2023/05/14/cfb8b73b224490acf0375bf3a5f35c4d.png');
                $table->number('height', '高度')->help('单位px。在内容中显示表情时图片的高度')->default(20);
                $table->number('width', '宽度')->help('单位px。在内容中显示表情时图片的宽度')->default(20);
            })->default(Settings::get('app_emoji_page_1', ''));
        });
        $form->tab('第二页', function (Form $form){
            $form->table('app_emoji_page_2','图片表情', function (NestedForm $table) {
                $table->text('alt', 'alt')->help('会把内容中[坏笑]替换成表情展示')->default('[坏笑]');
                $table->image('emoji', '表情')->help('<a href="https://img.mini.minisns.cn/2023/05/14/cfb8b73b224490acf0375bf3a5f35c4d.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/2023/05/14/cfb8b73b224490acf0375bf3a5f35c4d.png');
                $table->number('height', '高度')->help('单位px。在内容中显示表情时图片的高度')->default(20);
                $table->number('width', '宽度')->help('单位px。在内容中显示表情时图片的宽度')->default(20);
            })->default(Settings::get('app_emoji_page_2', ''));
        });
        $form->tab('第三页', function (Form $form){
            $form->table('app_emoji_page_3','图片表情', function (NestedForm $table) {
                $table->text('alt', 'alt')->help('会把内容中[坏笑]替换成表情展示')->default('[坏笑]');
                $table->image('emoji', '表情')->help('<a href="https://img.mini.minisns.cn/2023/05/14/cfb8b73b224490acf0375bf3a5f35c4d.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/2023/05/14/cfb8b73b224490acf0375bf3a5f35c4d.png');
                $table->number('height', '高度')->help('单位px。在内容中显示表情时图片的高度')->default(20);
                $table->number('width', '宽度')->help('单位px。在内容中显示表情时图片的宽度')->default(20);
            })->default(Settings::get('app_emoji_page_3', ''));
        });

        $form->action('settings/action/emoji');
        return $form;
    }

    public function emoji(Request $request){
        SiteUtils::forget_config_data();

        Settings::set('app_emoji_page_1', AdminUtils::_table_data($request->input('app_emoji_page_1')), true);
        Settings::set('app_emoji_page_2', AdminUtils::_table_data($request->input('app_emoji_page_2')), true);
        Settings::set('app_emoji_page_3', AdminUtils::_table_data($request->input('app_emoji_page_3')), true);

        Cache::forget('app_emoji_cache');
        Cache::forget('app_emoji_alt_str');
        $app_emoji_alt_str = '';
        $page1 = json_decode(AdminUtils::_table_data($request->input('app_emoji_page_1')) ?: '[]', true);
        $page2 = json_decode(AdminUtils::_table_data($request->input('app_emoji_page_2')) ?: '[]', true);
        $page3 = json_decode(AdminUtils::_table_data($request->input('app_emoji_page_3')) ?: '[]', true);
        $app_emoji_cache = [];
        $partern = '';
        if($page1){
            foreach ($page1 as $emoji){
                $key = _preg_prepare($emoji['alt']);
                $app_emoji_cache[$emoji['alt']] = $emoji;
                $app_emoji_alt_str .= '|'.$emoji['alt'];
                if(_empty_($partern)){
                    $partern = '$'.$key.'';
                }else{
                    $partern .= '|'.$key.'';
                }
            }
        }
        if($page2){
            foreach ($page2 as $emoji){
                $key = _preg_prepare($emoji['alt']);
                $app_emoji_cache[$emoji['alt']] = $emoji;
                $app_emoji_alt_str .= '|'.$emoji['alt'];
                if(_empty_($partern)){
                    $partern = '$'.$key.'';
                }else{
                    $partern .= '|'.$key.'';
                }
            }
        }
        if($page3){
            foreach ($page3 as $emoji){
                $key = _preg_prepare($emoji['alt']);
                $app_emoji_cache[$emoji['alt']] = $emoji;
                $app_emoji_alt_str .= '|'.$emoji['alt'];
                if(_empty_($partern)){
                    $partern = '$'.$key.'';
                }else{
                    $partern .= '|'.$key.'';
                }
            }
        }
        if($partern){
            $partern .= '$';
        }
        $app_emoji_cache['partern'] = $partern;

        Cache::put('app_emoji_cache', json_encode($app_emoji_cache));
        Cache::put('app_emoji_alt_str', $app_emoji_alt_str);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
